﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLayer;
using Library;

public partial class Administrator_ProductEdit : System.Web.UI.Page
{
    string cmd;
    string Id;

    protected void Page_Load(object sender, EventArgs e)
    {
        cmd = Request.QueryString["cmd"];

        switch (cmd)
        {
            case "new":
                rbStatus.Enabled = false;
                btnNew.Enabled = true;
                btnUpdate.Enabled = false;
                rbStatus.Enabled = false;
                break;
            case "edit":
                rbStatus.Enabled = true;
                btnNew.Enabled = false;
                btnUpdate.Enabled = true;
                Id = Request.QueryString["Id"];

                if (!IsPostBack)
                    FillData(Product.GetOne(Id));
                break;
        }
    }

    protected void FillData(Product product)
    {
        txtId.Text = product.Id;
        txtName.Text = product.Product_Name;
        txtPrice.Text = product.Price.ToString();
        txtBaoHanh.Text = product.Bao_hanh.ToString();
        txtDetail.Value = product.Detail;
        rblStock.Items.FindByValue(product.Stock.ToString()).Selected = true;
        Image1.ImageUrl = product.Image_Url;
        rbStatus.Items.FindByValue(product.Del_Status.ToString()).Selected = true;
    }

    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {
        if (CheckBox1.Checked)
        {
            fileUpload.Enabled = true;
        }
        else
        {
            fileUpload.Enabled = false;
        }
    }

    protected void btnNew_Click(object sender, EventArgs e)
    {
        Product product = new Product();
        product.Id = txtId.Text;
        product.Product_Name = txtName.Text;
        product.Price = Convert.ToInt32(txtPrice.Text);
        product.Bao_hanh = Convert.ToInt32(txtBaoHanh.Text);
        product.Detail = txtDetail.Value;
        product.Image_Url = fileUpload.FileName;

        try
        {
            Product.Insert(product);
            MessageBox.Show("Sản phẩm đã được thêm thành công!");
            Response.Redirect("Product.aspx");
        }
        catch (Exception ex)
        {
            MessageBox.Show("Lỗi !!! Mời bạn thử lại!");
            throw ex;
        }
    }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        Product product = new Product();
        product.Id = txtId.Text;
        product.Product_Name = txtName.Text;
        product.Price = Convert.ToInt32(txtPrice.Text);
        product.Bao_hanh = Convert.ToInt32(txtBaoHanh.Text);
        product.Detail = txtDetail.Value;
        product.Image_Url = fileUpload.FileName;
        product.Del_Status = 0;

        try
        {
            Product.Update(product);
            MessageBox.Show("Sản phẩm đã được sửa thành công!");
            Response.Redirect("Product.aspx");
        }
        catch (Exception ex)
        {
            MessageBox.Show("Lỗi !!! Mời bạn thử lại!");
            throw ex;
        }
    }

    protected void BbtnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("Product.aspx");
    }
}